﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLACQUY.BUS;
using QLACQUY.DTO;
using System.Collections;

namespace QLACQUY
{
    public partial class ThemPhieuGiaoHang : Form
    {
        public ThemPhieuGiaoHang()
        {
            InitializeComponent();
            
        }

        private void ThemPhieuGiaoHang_Load(object sender, EventArgs e)
        {
            //create the tree
            NhaSanXuatBUS nsxBUS = new NhaSanXuatBUS();
            
            ArrayList arr = (ArrayList)nsxBUS.layDanhSach();            
            for (int i = 0; i< arr.Count; i++)
            {
                treeView1.Nodes.Add(((NhaSanXuatDTO)arr[i]).TenNSX);                
            }
            //treeView1.Sort();
            treeView1.Nodes.Insert(0,"Tất cả");

            //get data for the auto complete text box
            AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
            AutoCompleteStringCollection DienThoaiCollection = new AutoCompleteStringCollection();
            AutoCompleteStringCollection DiaChiCollection = new AutoCompleteStringCollection();
            AutoCompleteStringCollection NoCuCollection = new AutoCompleteStringCollection();
            AutoCompleteStringCollection MaSoCollection = new AutoCompleteStringCollection();
            KhachHangBUS khBus = new KhachHangBUS();
            ArrayList arrKH = khBus.layDanhSach();
            for (int i = 0; i < arrKH.Count; i++ )
            {
                namesCollection.Add(((KhachHangDTO)arrKH[i]).TenKH);
                DienThoaiCollection.Add(((KhachHangDTO)arrKH[i]).DienThoai);
                DiaChiCollection.Add(((KhachHangDTO)arrKH[i]).DiaChi);
                NoCuCollection.Add(((KhachHangDTO)arrKH[i]).NoCu.ToString());
                MaSoCollection.Add(((KhachHangDTO)arrKH[i]).MaSo.ToString());

            }
            txtTenKH.AutoCompleteCustomSource = namesCollection;
            txtDiaChi.AutoCompleteCustomSource = DiaChiCollection;
            txtDienThoai.AutoCompleteCustomSource = DienThoaiCollection;
            txtNoCu.AutoCompleteCustomSource = NoCuCollection;
            txtMaSo.AutoCompleteCustomSource = MaSoCollection;
            txtTenKH.Focus();
        }

        private void btnLapPhieu_Click(object sender, EventArgs e)
        {            
            //save khachhang
            errorProvider1.Clear();
            int err = 0;
            if (txtTenKH.Text == "")
            {
                errorProvider1.SetError(txtTenKH, "Tên khách hàng không được để trống ");
                err = 1;                    
            }
            if (txtDienThoai.Text == "")
            {
                errorProvider1.SetError(txtDienThoai, "Điện thoại không được để trống ");
                err = 1;
            }
            if (txtMaSo.Text == "")
            {
                errorProvider1.SetError(txtMaSo, "Mã số không được để trống ");
                err = 1;
            }

            if (err != 0)
                return;
            KhachHangDTO khDTO = new KhachHangDTO();
            khDTO.TenKH = txtTenKH.Text;
            khDTO.DiaChi = txtDiaChi.Text;
            khDTO.DienThoai = txtDienThoai.Text;
            khDTO.MaSo = txtMaSo.Text;
            double temp = 0;
            double.TryParse(txtNoCu.Text, out temp);
            double temp2 = 0;
            double.TryParse(txtKhachTra.Text,out temp2);
            khDTO.NoCu = (double.Parse(lbTongCong.Text)+temp)- temp2;

            int makh;
            KhachHangBUS khBUS = new KhachHangBUS();
            if (khBUS.checkExists(khDTO))
                makh = khBUS.capNhatKhachHang(khDTO);
            else
                makh = khBUS.themKhachHang(khDTO);  
         
            //save phieugiaohang
            if(makh == 0)
            {
                MessageBox.Show("Thêm khách hàng thất bại!");
                return;
            }
            PhieuGiaoHangDTO phieuDTO = new PhieuGiaoHangDTO();
            phieuDTO.MaKH = makh;
            phieuDTO.MaNVGiaoHang = 0;
            phieuDTO.NgayLapPhieu = dateTimePicker1.Value;
            phieuDTO.TongTien = double.Parse(lbTongCong.Text);
            phieuDTO.NoCu = temp;
            phieuDTO.TienKhachTra = temp2;
            PhieuGiaoHangBUS phieuBUS = new PhieuGiaoHangBUS();
            int maPhieu = phieuBUS.themPhieuGiaoHang(phieuDTO);

            //save chi tiet phieugiaohang
            if (maPhieu == 0)
            {
                MessageBox.Show("Thêm phiếu giao hàng thất bại!");
                return;
            }
            CTPhieuGiaoHangBUS ctPhieuBUS = new CTPhieuGiaoHangBUS();
            for (int i = 0; i < dataGridView1.Rows.Count-1; i++ )
            {
                CTPhieuGiaoHangDTO CTPhieuDTO = new CTPhieuGiaoHangDTO();
                CTPhieuDTO.MaPhieuGiaoHang = maPhieu;
                CTPhieuDTO.MaSanPham = (int)dataGridView1.Rows[i].Cells["MaSP"].Value;
                CTPhieuDTO.SoLuong = int.Parse(dataGridView1.Rows[i].Cells["SoLuong"].Value.ToString());
                CTPhieuDTO.DonGia = double.Parse(dataGridView1.Rows[i].Cells["DonGia"].Value.ToString());
                CTPhieuDTO.ThanhTien = CTPhieuDTO.SoLuong * CTPhieuDTO.DonGia;
                ctPhieuBUS.themCTPhieuGiaoHang(CTPhieuDTO);                
            }
            if(ckHienBaoCao.Checked)
            {
                BaoCao2 frm = new BaoCao2(lbChuTongNo.Text, makh, maPhieu);
                frm.Show();
            }
            else
                MessageBox.Show("Lưu thành công!");
            
            

        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            
            panel1.Controls.Clear();
            ckCheckALL.Checked = false;
            string nsx = e.Node.Text;
            SanPhamBUS bus = new SanPhamBUS();
            ArrayList arr = null;
            if (nsx == "Tất cả")            
                arr = bus.layDanhSach();            
            else
                arr = bus.layDanhSachTheoNhaSanXuat(nsx);
            
            int x = 0;
            int y = 0;
            for (int i = 0; i < arr.Count; i++ )
            {
                UC_TrinhBaySanPham uc = new UC_TrinhBaySanPham();
                uc.ImageName = ((SanPhamCRT)arr[i]).HinhAnh;
                uc.NhaSanXuat = ((SanPhamCRT)arr[i]).NSX;
                uc.DonGiaLe = ((SanPhamCRT)arr[i]).DonGiaLe;
                uc.DonGiaSy = ((SanPhamCRT)arr[i]).DonGiaSy;
                uc.MaSP = ((SanPhamCRT)arr[i]).MaSanPham;
                uc.MoTa = ((SanPhamCRT)arr[i]).MoTa;
                uc.QuyCach = ((SanPhamCRT)arr[i]).QuyCach;  
                uc.Location = new System.Drawing.Point(x, y);
                panel1.Controls.Add(uc);
                x = 0;// (x + 250) % 500;
                //if (x == 0)                
                y = y + 23;
                
            }

        }

        private void btnDuaVaoDanhSach_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < panel1.Controls.Count; i++ )
            {
                UC_TrinhBaySanPham uc = (UC_TrinhBaySanPham)panel1.Controls[i];
                if (uc.getCheck())
                {
                    //DataGridViewRow r = new DataGridViewRow();
                    //r.CreateCells(dataGridView1);
                    
                    int k = dataGridView1.Rows.Add();
                    if(k == 0)
                        dataGridView1.Rows[k].Cells["STT"].Value = 1;
                    else
                        dataGridView1.Rows[k].Cells["STT"].Value = int.Parse(dataGridView1.Rows[k-1].Cells["STT"].Value.ToString()) + 1;
                    dataGridView1.Rows[k].Cells["QuyCach"].Value = uc.QuyCach;
                    dataGridView1.Rows[k].Cells["NhanHieu"].Value = uc.NhaSanXuat;
                    dataGridView1.Rows[k].Cells["DonGia"].Value = uc.getSelectedPrice();
                    dataGridView1.Rows[k].Cells["MoTa"].Value = uc.MoTa;
                    dataGridView1.Rows[k].Cells["SoLuong"].Value = 1;
                    dataGridView1.Rows[k].Cells["ThanhTien"].Value = uc.getSelectedPrice();
                    dataGridView1.Rows[k].Cells["MaSP"].Value = uc.MaSP;
                    
                }
            }
            tinhTongTien();
        }

        private void ckCheckALL_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < panel1.Controls.Count; i++ )
            {
                UC_TrinhBaySanPham uc = (UC_TrinhBaySanPham)panel1.Controls[i];
                uc.setCheck(ckCheckALL.Checked);
            }
        }

       
        public double tinhTongTien()
        {
            try
            {
                double sum = 0;
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    string sTien = dataGridView1.Rows[i].Cells["ThanhTien"].Value.ToString();
                    if (sTien != "")
                    {
                        sum += double.Parse(sTien);
                    }
                }
                //cong no cu
                double nocu = 0;
                double.TryParse(txtNoCu.Text, out nocu);
                //sum += nocu;

                lbNoCu.Text = nocu.ToString();
                lbTienChu.Text = Global.DocTienBangChu((long)sum, " đồng");
                lbTongCong.Text = sum.ToString();
                double khachtra = 0;
                double.TryParse(txtKhachTra.Text, out khachtra);
                double tongno =  Math.Abs((sum + nocu) - khachtra);
                lbTongNo.Text =tongno.ToString();
                lbChuTongNo.Text = Global.DocTienBangChu((long)tongno, " đồng");

                return sum;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return 0;
            }
            
        }

        
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int rowIndex = e.RowIndex;
                string sSoLuong = dataGridView1.Rows[rowIndex].Cells["SoLuong"].Value.ToString();
                string sDonGia = dataGridView1.Rows[rowIndex].Cells["DonGia"].Value.ToString();
                if (sSoLuong != "" && sDonGia != "")
                {

                    double donGia = double.Parse(sDonGia);
                    int soLuong = int.Parse(sSoLuong);
                    double thanhtien = soLuong * donGia;
                    dataGridView1.Rows[rowIndex].Cells["ThanhTien"].Value = thanhtien;
                    tinhTongTien();
                }



            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Giá trị cập nhật không hợp lệ!");

            }
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            lbTongCong.Text = tinhTongTien().ToString();
        }

        private void txtTenKH_Leave(object sender, EventArgs e)
        {
            if (txtTenKH.Text !="")
            {
                KhachHangBUS khBUS = new KhachHangBUS();
                KhachHangDTO khDTO = khBUS.layKhachHangTheoTen(txtTenKH.Text);
                if (khDTO != null)
                {
                    txtDiaChi.Text = khDTO.DiaChi;
                    txtDienThoai.Text = khDTO.DienThoai;
                    txtNoCu.Text = khDTO.NoCu.ToString();
                    txtMaSo.Text = khDTO.MaSo;
                    txtDienThoai.Focus();
                    tinhTongTien();
                }
            }
        }

        private void txtNoCu_Leave(object sender, EventArgs e)
        {
            tinhTongTien();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            txtTenKH.Text = "";
            txtDienThoai.Text = "";
            txtDiaChi.Text = "";
            txtNoCu.Text = "";
            lbTienChu.Text = "";
            lbTongCong.Text = "0";
            txtTenKH.Focus();
            txtKhachTra.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            treeView1.Sort();
        }

        

        private void txtKhachTra_Leave_1(object sender, EventArgs e)
        {
            tinhTongTien();
        }

        private void txtMaSo_Leave(object sender, EventArgs e)
        {
            if (txtMaSo.Text != "")
            {
                KhachHangBUS khBUS = new KhachHangBUS();
                KhachHangDTO khDTO = khBUS.layKhachHangTheoMaSo(txtMaSo.Text);
                if (khDTO != null)
                {
                    txtDiaChi.Text = khDTO.DiaChi;
                    txtDienThoai.Text = khDTO.DienThoai;
                    txtNoCu.Text = khDTO.NoCu.ToString();
                    txtTenKH.Text = khDTO.TenKH;
                    txtDienThoai.Focus();
                    tinhTongTien();
                }
            }
        }

       

        
    }
}
